*Preparation of variables for the analysis on the determinants of individual decisions to abandon the mainstream Left 

tab cntry, gen (dcntry)
tab cntelec, gen (dcntelec)

recode educ 9=.

*-------------------
*Social class
*-------------------

tab clasdt
tab clasdt, nol
recode clasdt 1/2=1 3/8=2, gen (clas2_wcserpro)
label var clas2_wcserpro "Social class, 2 cat, servpro" 
lab def clas2_wcserpro 1 "Workers" 2 "Non-workers" 
lab val clas2_wcserpro clas2_wcserpro
tab clas2_wcserpro

recode clas2_wcserpro 2=0 1=1, gen (clas2fin)
label var clas2fin "Social class, 2 cat, final" 
lab def clas2fin 0 "Non-workers" 1 "Workers" 
lab val clas2fin clas2fin
tab clas2fin

tab clasdt
tab clasdt, nol
recode clasdt 1/2=1 3=3 4=2 5/8=3, gen (clas3soc_wcserpro) 
label var clas3soc_wcserpro "Social class, 3 cat, socio-cu" 
lab def clas3soc_wcserpro 1 "Workers" 2 "Socio-cultural" 3"Other non-workers" 
lab val clas3soc_wcserpro clas3soc_wcserpro
tab clas3soc_wcserpro


*-------------------
*Dependent variable
*-------------------

gen switch_x=.
replace switch_x=0 if preparty5abs==2 & party5abs==2
replace switch_x=1 if (preparty5abs==2 & party5abs==0) | (preparty5abs==2 & party5abs==3) | (preparty5abs==2 & party5abs==4)  | (preparty5abs==2 & party5abs==10) | (preparty5abs==2 & party5abs==11) /*voted SD previous elec, but not at current*/
label var switch_x "Away from mainstream left" 
lab def switch_x 0 "Stable SD supporters" 1"Away from SD"
lab val switch_x switch_x
tab switch_x


*----------------------------
*Class profile of new voters
*----------------------------

*Identify new voters among the electorate of the mainstream Left 
gen turn_x1=.
replace turn_x1=0 if preparty5abs==2 & party5abs==2
replace turn_x1=1 if (preparty5abs==0 & party5abs==2) | (preparty5abs==3 & party5abs==2) | (preparty5abs==4 & party5abs==2) | (preparty5abs==10 & party5abs==2) | (preparty5abs==11 & party5abs==2)   /*not voted SD previous elec, but yes at current*/
replace turn_x1=1 if (preparty5abs==5 & party5abs==2) /*Non-eligible citizens at previous election*/
label var turn_x1 "Turn to the mainstream left" 
lab def turn_x1 0 "Stable SD supporters" 1"To SD"
lab val turn_x1 turn_x1
tab turn_x1

*Numerator: percentage workers among new voters for the mainstream Left 
tab cntelec clas2_wcserpro if turn_x1==1, row 

by cntelec, sort: egen inwcpool_prep1= mean(100*inlist (clas2_wcserpro,1)) if turn_x1==1 & clas2_wcserpro!=. 
label var inwcpool_prep1 "Inflow perc wcserpro in mL electorate, prep1"

by cntelec, sort: egen inwcpool1= mean(inwcpool_prep1)

*Denominator: percentage of workers in the potential population of new voters (did not vote for the mainstream Left in the previous election)
tab clas2_wcserpro if party5abs!=. & preparty5abs!=2 & preparty5abs!=6 & preparty5abs!=200 

by cntelec, sort: egen pool_prep1= mean(100*inlist (clas2_wcserpro,1)) if clas2_wcserpro!=. & party5abs!=. & preparty5abs!=2 & preparty5abs!=6 & preparty5abs!=200
label var pool_prep1 "Perc serpro pool, prep1"

by cntelec, sort: egen pool1= mean(pool_prep1)

*Ratio 
by cntelec, sort: gen inratiopool1=inwcpool1/pool1

*Table 3
by cntelec, sort: tab inratiopool1
tabstat inratiopool1, by (cntelec) 

*Center the variable
summarize inratiopool1 if switch_x!=. & clas2_wcserpro!=.  &   unionm!=.  & age!=.  & gender!=. & locat!=., meanonly
gen c_inratiopool1 = inratiopool1 - r(mean) if switch_x!=. & clas2_wcserpro!=.  &   unionm!=.  & age!=.  & gender!=. & locat!=. 


*----------------------------
*Party competition
*----------------------------

*competition from radical left
gen crl=.
replace crl=	0	if cntelec==	"AUS_2004" 
replace crl=	0	if cntelec==	"AUS_2007" 
replace crl=	0	if cntelec==	"AUS_2013" 
replace crl=	0	if cntelec==	"AUT_2013"
replace crl=	10.30	if cntelec==	"DNK_2001"
replace crl=	5.5	if cntelec==	"ESP_2004"
replace crl=	7.7	if cntelec==	"FRA_2012"
replace crl=	5.1	if cntelec==	"DEU12002" 
replace crl=	4.0	if cntelec==	"DEU_2005" 
replace crl=	8.7	if cntelec==	"DEU_2009" 
replace crl=	11.9	if cntelec==	"DEU_2013" 
replace crl=	10.9	if cntelec==	"FIN_2003" 
replace crl=	9.9	if cntelec==	"FIN_2007" 
replace crl=	8.8	if cntelec==	"FIN_2011" 
replace crl=	8.1	if cntelec==	"FIN_2015" 
replace crl=	13.2	if cntelec==	"GRC_2009" 
replace crl=	12.1	if cntelec==	"GRC_2012" 
replace crl=	2.6	if cntelec==	"IRL_2002" 
replace crl=	6.5	if cntelec==	"IRL_2007" 
replace crl=	9.1	if cntelec==	"ISL_2003" 
replace crl=	8.8	if cntelec==	"ISL_2007" 
replace crl=	14.3	if cntelec==	"ISL_2009"  
replace crl=	21.7	if cntelec==	"ISL_2013" 
replace crl=	3.5	if cntelec==	"NLD_2002"
replace crl=	6.3	if cntelec==	"NLD_2006"
replace crl=	16.6	if cntelec==	"NLD_2010"
replace crl=	6.0	if cntelec==	"NOR_2001" 
replace crl=	12.5	if cntelec==	"NOR_2005" 
replace crl=	8.8	if cntelec==	"NOR_2009" 
replace crl=	6.2	if cntelec==	"NOR_2013" 
replace crl=	7.7	if cntelec==	"NZL_2002" 
replace crl=	0.0	if cntelec==	"NZL_2008" 
replace crl=	0.0	if cntelec==	"NZL_2011" 
replace crl=	0.0	if cntelec==	"NZL_2014" 
replace crl=	12.0	if cntelec==	"SWE_2002" 
replace crl=	8.4	if cntelec==	"SWE_2006" 
replace crl=	5.6	if cntelec==	"SWE_2014" 
replace crl=	0	if cntelec==	"GBR_2005"
replace crl=	0	if cntelec==	"GBR_2015" 
replace crl=	0.0	if cntelec==	"CHE_2011"
tab crl

*competition from radical right
gen crr=.
replace crr=	4.3	if cntelec==	"AUS_2004" 
replace crr=	0	if cntelec==	"AUS_2007" 
replace crr=	0	if cntelec==	"AUS_2013" 
replace crr=	28.2	if cntelec==	"AUT_2013"
replace crr=	9.8	if cntelec==	"DNK_2001"
replace crr=	0.0	if cntelec==	"ESP_2004"
replace crr=	4.7	if cntelec==	"FRA_2012"
replace crr=	0.0	if cntelec==	"DEU12002" 
replace crr=	0.0	if cntelec==	"DEU_2005" 
replace crr=	0.0	if cntelec==	"DEU_2009" 
replace crr=	0.0	if cntelec==	"DEU_2013" 
replace crr=	0.0	if cntelec==	"FIN_2003" 
replace crr=	0.0	if cntelec==	"FIN_2007" 
replace crr=	4.1	if cntelec==	"FIN_2011" 
replace crr=	19.1	if cntelec==	"FIN_2015" 
replace crr=	3.8	if cntelec==	"GRC_2009" 
replace crr=	5.6	if cntelec==	"GRC_2012" 
replace crr=	0.0	if cntelec==	"IRL_2002" 
replace crr=	0.0	if cntelec==	"IRL_2007" 
replace crr=	0.0	if cntelec==	"ISL_2003" 
replace crr=	0.0	if cntelec==	"ISL_2007" 
replace crr=	0.0	if cntelec==	"ISL_2009"  
replace crr=	0.0	if cntelec==	"ISL_2013" 
replace crr=	0.0	if cntelec==	"NLD_2002"
replace crr=	5.6	if cntelec==	"NLD_2006"
replace crr=	5.9	if cntelec==	"NLD_2010"
replace crr=	15.3	if cntelec==	"NOR_2001" 
replace crr=	14.6	if cntelec==	"NOR_2005" 
replace crr=	22.1	if cntelec==	"NOR_2009" 
replace crr=	22.9	if cntelec==	"NOR_2013" 
replace crr=	4.3	if cntelec==	"NZL_2002" 
replace crr=	5.7	if cntelec==	"NZL_2008" 
replace crr=	4.1	if cntelec==	"NZL_2011" 
replace crr=	6.6	if cntelec==	"NZL_2014" 
replace crr=	0.0	if cntelec==	"SWE_2002" 
replace crr=	0.0	if cntelec==	"SWE_2006" 
replace crr=	5.7	if cntelec==	"SWE_2014" 
replace crr=	0	if cntelec==	"GBR_2005"
replace crr=	3.1	if cntelec==	"GBR_2015" 
replace crr=	28.9	if cntelec==	"CHE_2011"
tab crr

*competition from Greens
gen cg=.
replace cg=	5.0	if cntelec==	"AUS_2004" 
replace cg=	7.2	if cntelec==	"AUS_2007" 
replace cg=	11.8	if cntelec==	"AUS_2013" 
replace cg=	10.4	if cntelec==	"AUT_2013"
replace cg=	0.0	if cntelec==	"DNK_2001"
replace cg=	0.0	if cntelec==	"ESP_2004"
replace cg=	3.2	if cntelec==	"FRA_2012"
replace cg=	6.7	if cntelec==	"DEU12002" 
replace cg=	8.6	if cntelec==	"DEU_2005" 
replace cg=	8.1	if cntelec==	"DEU_2009" 
replace cg=	10.7	if cntelec==	"DEU_2013" 
replace cg=	7.3	if cntelec==	"FIN_2003" 
replace cg=	8.0	if cntelec==	"FIN_2007" 
replace cg=	8.5	if cntelec==	"FIN_2011" 
replace cg=	7.3	if cntelec==	"FIN_2015" 
replace cg=	0.0	if cntelec==	"GRC_2009" 
replace cg=	2.5	if cntelec==	"GRC_2012" 
replace cg=	2.8	if cntelec==	"IRL_2002" 
replace cg=	3.9	if cntelec==	"IRL_2007" 
replace cg=	0.0	if cntelec==	"ISL_2003" 
replace cg=	0.0	if cntelec==	"ISL_2007" 
replace cg=	3.3	if cntelec==	"ISL_2009"  
replace cg=	0.0	if cntelec==	"ISL_2013" 
replace cg=	7.3	if cntelec==	"NLD_2002"
replace cg=	5.1	if cntelec==	"NLD_2006"
replace cg=	4.6	if cntelec==	"NLD_2010"
replace cg=	0.0	if cntelec==	"NOR_2001" 
replace cg=	0.0	if cntelec==	"NOR_2005" 
replace cg=	0.0	if cntelec==	"NOR_2009" 
replace cg=	0.0	if cntelec==	"NOR_2013" 
replace cg=	5.2	if cntelec==	"NZL_2002" 
replace cg=	5.3	if cntelec==	"NZL_2008" 
replace cg=	6.7	if cntelec==	"NZL_2011" 
replace cg=	11.1	if cntelec==	"NZL_2014" 
replace cg=	4.5	if cntelec==	"SWE_2002" 
replace cg=	4.6	if cntelec==	"SWE_2006" 
replace cg=	7.3	if cntelec==	"SWE_2014" 
replace cg=	0.0	if cntelec==	"GBR_2005"
replace cg=	0.0	if cntelec==	"GBR_2015" 
replace cg=	9.6	if cntelec==	"CHE_2011"
tab cg

*Center the variables
summarize crl if switch_x!=. & clas2_wcserpro!=.  &   unionm!=.  & age!=.  & gender!=. & locat!=., meanonly 
gen c_crl = crl - r(mean) if switch_x!=. & clas2_wcserpro!=.  &   unionm!=.  & age!=.  & gender!=. & locat!=. 

summarize crr if switch_x!=. & clas2_wcserpro!=.  &   unionm!=.  & age!=.  & gender!=. & locat!=., meanonly 
gen c_crr = crr - r(mean) if switch_x!=. & clas2_wcserpro!=.  &   unionm!=.  & age!=.  & gender!=. & locat!=. 

summarize cg if switch_x!=. & clas2_wcserpro!=.  &   unionm!=.  & age!=.  & gender!=. & locat!=., meanonly 
gen c_cg = cg - r(mean) if switch_x!=. & clas2_wcserpro!=.  &   unionm!=.  & age!=.  & gender!=. & locat!=. 


*----------------------------
*Incumbency
*----------------------------

gen crisisinc=0
replace crisisinc=1 if cntelec=="ISL_2013"
replace crisisinc=1 if cntelec=="GRC_2012"
label var crisisinc "Crisis and incumbency" 
lab def crisisinc 0 "No" 1 "Crisis incumbent" 
lab val crisisinc crisisinc
tab crisisinc

gen govtparty=0
replace govtparty=1 if cntelec=="AUS_2013"
replace govtparty=1 if cntelec=="AUT_2013"
replace govtparty=1 if cntelec=="DNK_2001"
replace govtparty=1 if cntelec=="DEU_2002"
replace govtparty=1 if cntelec=="DEU_2005"
replace govtparty=1 if cntelec=="DEU_2009"
replace govtparty=1 if cntelec=="FIN_2003"
replace govtparty=1 if cntelec=="FIN_2007"
replace govtparty=1 if cntelec=="FIN_2015"
replace govtparty=1 if cntelec=="GRC_2012"
replace govtparty=1 if cntelec=="ISL_2009"
replace govtparty=1 if cntelec=="ISL_2013"
replace govtparty=1 if cntelec=="NLD_2002"
replace govtparty=1 if cntelec=="NLD_2010"
replace govtparty=1 if cntelec=="NOR_2009"
replace govtparty=1 if cntelec=="NOR_2013"
replace govtparty=1 if cntelec=="NZL_2002"
replace govtparty=1 if cntelec=="NZL_2008"
replace govtparty=1 if cntelec=="SWE_2002"
replace govtparty=1 if cntelec=="SWE_2006"
replace govtparty=1 if cntelec=="GBR_2005"
replace govtparty=1 if cntelec=="CHE_2011"
label var govtparty "Party in government" 
lab def govtparty 0 "No" 1 "Yes" 
lab val govtparty govtparty
tab govtparty
